@extends('common.admin_template')

@section('content')
    <div class="boxWidth">
        @include('common.notice_msg')
        <div class="row">
            <div class="col-xs-4 boxPaddingLeft">
                <div class="addFormItemHead">添加新{{$type == 'xcx'?'小程序分类':$type == 'tag'?'页面标签':'页面分类'}}</div>
                <form method="post" action="{{url('admin/tag/save')}}">
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                @if($type != 'service')
                <input type="hidden" name="Tag[type]" value="{{ $type }}">
                @endif
                <div class="addInput" id="TagNameBox">
                    <div class="addInputText">分类名称</div>
                    <input type="text" class="form-control" id="TagName" name="Tag[name]" placeholder="请输入{{$type == 'xcx'?'小程序分类':$type == 'tag'?'页面标签':'页面分类'}}分类名称">
                </div>
                <div class="addInput" style="{{$type != 'service'?'display:none':''}}">
                    <div class="addInputText">筛选名称</div>
                    <input type="text" class="form-control" name="Tag[filter]" placeholder="请填写分类筛选名称"/>
                </div>
                @if($type == 'service')
                <div class="addInput">
                    <div class="addInputText">功能类型
                        <select name="Tag[type]" style="margin-left: 20px">
                            <option value="service">服务</option>
                            <option value="shop">商品</option>
                            <option value="coupon">优惠券入口</option>
                            <option value="couponCat">优惠券</option>
                        </select>
                    </div>
                </div>
                @endif
                <div class="addInput">
                    <div class="addInputText">分类排序</div>
                    <input type="text" class="form-control" name="Tag[order_id]" placeholder="不填则自动生成排序"/>
                </div>
                <div class="addInput" id="TagCoverBox">
                    <div class="addInputText">分类封面</div>
                    <input type="text" class="form-control" id="TagCover" name="Tag[more]" placeholder="请输入图片地址或上传图片"/>
                </div>
                <div class="addInput row">
                    <div class="col-xs-6"><button class="btn btn-primary addInputBtn" type="submit" id="formSubmit">添加新{{$type == 'xcx'?'小程序分类':$type == 'tag'?'页面标签':'页面分类'}}</button></div>
                    <div class="col-xs-6" id="contentImg"><button><img src="{{asset('img/upload.png')}}">上传图片</button></div>
                </div>
                </form>
            </div>
            <div class="col-xs-8 boxPaddingRight">
                <table class="table table-hover adminTableBg">
                    <thead>
                    <tr>
                        <th class="col-xs-1">ID</th>
                        @if($type == 'service')
                            <th class="col-xs-4">分类名</th>
                            <th class="col-xs-2">类型</th>
                            <th class="col-xs-2">筛选名</th>
                        @else
                            <th class="col-xs-6">{{$type == 'tag'?'标签':'分类'}}名</th>
                        @endif
                        <th class="col-xs-1">排序</th>
                        <th class="col-xs-2">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php $tagType = Array("service"=>"服务","shop"=>"商品","coupon"=>"优惠券入口","couponCat"=>"优惠券") ?>
                    @foreach($Tags as $item)
                    <tr>
                        <th class="col-xs-1 itemTd">{{$item->id}}</th>
                        @if($type == 'service')
                            <th class="col-xs-4">{{$item->name}}</th>
                            <th class="col-xs-2">{{$tagType[$item->type]}}</th>
                            <th class="col-xs-2">{{$item->filter}}</th>
                        @else
                            <th class="col-xs-8 itemTd">{{$item->name}}</th>
                        @endif
                        <th class="col-xs-1 itemTd">{{$item->order_id}}</th>
                        <th class="col-xs-2 itemTd">
                            <a href="{{url('admin/tag/edit',['id' => $item->id])}}"><span id="addInput">修改</span></a>
                            @if($type != 'xcx')
                            <a href="{{url('admin/tag/del',['id' => $item->id])}}"><span class="redColor" id="delInput" onclick="if(!confirm('是否确认删除？'))return false">删除</span></a>
                            @endif
                        </th>
                    </tr>
                    @endforeach
                    </tbody>
                </table>
                <div class="pull-right">
                    {{$Tags->render()}}
                </div>
            </div>
        </div>
    </div>
@stop
@section('js')
    <script src="{{asset('js/plupload/plupload.full.min.js')}}"></script>
    <script>
        (function () {
            $('#formSubmit').on('click',function (e) {
                if(!$('#TagName').val().replace(/(^\s*)|(\s*$)/g, "")){
                    $('#TagNameBox').addClass('has-error');
                    e.preventDefault();
                }
            });
            var picName = new Date();
            picName = picName.getFullYear().toString()+((picName.getMonth()+1)<10?'0'+(picName.getMonth()+1):(picName.getMonth()+1))+(picName.getDate()<10?'0'+picName.getDate():picName.getDate().toString())+'_'+(picName.getHours()<10?'0'+picName.getHours():picName.getHours().toString())+(picName.getMinutes()<10?'0'+picName.getMinutes():picName.getMinutes().toString())+(picName.getSeconds()<10?'0'+picName.getSeconds():picName.getSeconds().toString());

            var uploader = new plupload.Uploader({
                browse_button : 'contentImg',
                runtimes : 'html5,flash,silverlight,html4',
                url : "{{asset('api/upload.php')}}",
                flash_swf_url : "{{asset('js/plupload/Moxie.swf')}}",
                silverlight_xap_url : "{{asset('js/plupload/Moxie.xap')}}",
                unique_names : true,
                init: {
                    PostInit: function() {},
                    BeforeUpload:function(){
                        uploader.setOption("multipart_params",{
                            'key': picName
                        });
                    },
                    QueueChanged:function(){
                        uploader.start();
                    },
                    FileUploaded:function(up, file, res){
                        if(res.response){
                            var msg = JSON.parse(res.response);
                            $('#TagCover').val("{{asset('upload')}}"+"/"+msg.name)
                        }
                    },
                    UploadComplete: function(up, files) {
                        console.log('complete')
                    },
                    Error : function(up, err) {
                        console.log('fail')
                    }
                }
            });
            uploader.init();
        })();
    </script>
@stop